package com.chenss.dragonserapp.activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;
import com.chenss.dragonserapp.R;
import com.chenss.dragonserapp.base.BaseActivity;
import com.chenss.dragonserapp.fragment.GoodsFragment;
import com.chenss.dragonserapp.fragment.OrderFragment;
import com.chenss.dragonserapp.fragment.ServiceFragment;

public class MainActivity extends BaseActivity{
    private TextView tv_order;
    private TextView tv_service;
    private TextView tv_goods;
    private FrameLayout ly_content;

    private OrderFragment f1;
    private GoodsFragment f2;
    private ServiceFragment f3;
    private FragmentManager fragmentManager;
    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView(View view) {
        tv_order = findView(R.id.tv_order);
        tv_goods = findView(R.id.tv_goods);
        tv_service = findView(R.id.tv_service);
        ly_content =findView(R.id.ly_content);

        tv_order.setOnClickListener(this);
        tv_service.setOnClickListener(this);
        tv_goods.setOnClickListener(this);

        tv_order.performClick();


    }


    //重置所有文本的选中状态
    public void selected(){
        tv_order.setSelected(false);
        tv_service.setSelected(false);
        tv_goods.setSelected(false);
    }

    //隐藏所有Fragment
    public void hideAllFragment(FragmentTransaction transaction){
        if(f1!=null){
            transaction.hide(f1);
        }
        if(f2!=null){
            transaction.hide(f2);
        }
        if(f3!=null){
            transaction.hide(f3);
        }

    }
    @Override
    public void doBusiness(Context mContext) {

    }

    @Override
    public void widgetClick(View v) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        hideAllFragment(transaction);
        switch(v.getId()){
            case R.id.tv_order:
                selected();
                tv_order.setSelected(true);
                if(f1==null){
                    f1 = new OrderFragment();
                    transaction.add(R.id.ly_content,f1);
                }else{
                    transaction.show(f1);
                }
                break;
            case R.id.tv_goods:
                selected();
                tv_goods.setSelected(true);
                if(f2==null){
                    f2 = new GoodsFragment();
                    transaction.add(R.id.ly_content,f2);
                }else{
                    transaction.show(f2);
                }
                break;
            case R.id.tv_service:
                selected();
                tv_service.setSelected(true);
                if(f3==null){
                    f3 = new ServiceFragment();
                    transaction.add(R.id.ly_content,f3);
                }else{
                    transaction.show(f3);
                }
                break;



        }

        transaction.commit();
    }

}
